import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: "诸葛亮",
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: "蔡文姬",
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: "妲己",
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: "鲁班",
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  getters: {
    /* 为true 长度加一 */
    isshowAll(state) {
      return state.goodList.reduce((pre, item) => {
        if (item.checked) return pre + 1
         return pre
              },0)
    },
    /* 求和 */
    itemTal(state) {
    return   state.goodList.reduce((pre, item) => {
      if (item.checked) return item.num * item.price + pre
      return pre
      },0)
    }
  },
  mutations: {
    //删除
    delgoodList(state, payload) {
      state.goodList = state.goodList.filter((item) => item.name !== payload);
    },
    //button按钮增加减少
    addFn(state, payload) {
      state.goodList.forEach((item) => {
        if (item.name === payload.name) {
          item.num += payload.num;
        }
      });
    },
    //和小选框绑定状态
    isshow(state, payload) {
      const oneItem = state.goodList.find((item) => (item.name === payload.name));
      oneItem.checked = !oneItem.checked;
    },
    itemShow(state, payload) {
      state.goodList.forEach(item=>item.checked = payload)
    }
  
  },
  actions: {},
  modules: {},
});

